package com.training1.core.web.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;



import com.training1.core.entity.FunButtonEntity;
import com.training1.core.entity.FunRoleEntity;
import com.training1.core.entity.RoleEntity;
import com.training1.core.modules.service.RoleService;


@Controller
@RequestMapping("selectRole")
public class SelectRoleController {

	@Resource
	private RoleService roleService;
	
	/**
	 * 前往培训管理
	 */
	@RequestMapping("exe")
	public String exe(Model model){;
		return "lizeyu/biaoqian";
	}
	
	/**
	 * 前往查询页面
	 */
	@RequestMapping("s")
	public String gos(Model model){
		List<RoleEntity>list=roleService.find(null);
		model.addAttribute("list", list);
		return "role/selectRole";
	}
	
	/**
	 * 前往删除页面
	 */
	@RequestMapping("d")
	public String god(){
		return "role/deleteRole";
	}
	
	/**
	 * 前往修改页面
	 */
	@RequestMapping("u")
	public String gou(){
		return "role/updateRole";
	}
	
	/**
	 * 前往角色功能设置页面
	 */
	@RequestMapping("f")
	public String gof(Model model){
		List<FunButtonEntity> list=roleService.findAllFunButton();
		for(int i=0;i<list.size();i++){
			list.get(i).setMenuName(list.get(i).getMenuName1());
			if(list.get(i).getMenuName2()!=""&&list.get(i).getMenuName2()!=null){	
				list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName2());
			}
			if(list.get(i).getMenuName3()!=""&&list.get(i).getMenuName3()!=null){	
				list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName3());
			}
			if(list.get(i).getMenuName4()!=""&&list.get(i).getMenuName4()!=null){	
				list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName4());
			}
		}
		model.addAttribute("list", list);
		return "role/funRole";
	}
	
	/**
	 * 前往新增页面
	 */
	@RequestMapping("a")
	public String goa(){
		return "role/addRole";
	}
	//查找页面传值
	@RequestMapping("find")
	@ResponseBody
	public List<RoleEntity> find(String roleName){
		List<RoleEntity>list=roleService.find(roleName);
		return list;
	}
	//前往删除页面
	@RequestMapping("delete")
	public String delete(String roleId,Model model){
		model.addAttribute("roleName", roleService.findRoleName(roleId));
		model.addAttribute("roleId", roleId);
		return "role/deleteRole";
	}
	//删除角色
	@RequestMapping(value ="deleteRole",method=RequestMethod.GET,produces = "text/plain")
	@ResponseBody
	public String deleteRolet(String roleId){
		if(roleId==""||roleId==null){
			return "2";
		}
		Integer i=roleService.findExist(roleId);
		if(i!=0){
			return "0";
		}else{
			roleService.deleteRole(roleId);
			return "1";
		}
	}
	//前往修改页面
	@RequestMapping("update")
	public String update(String roleId,Model model){
		model.addAttribute("roleName", roleService.findRoleName(roleId));
		model.addAttribute("roleId", roleId);
		return "role/updateRole";
	}
	//修改角色
	@RequestMapping(value ="updateRole",method=RequestMethod.GET,produces = "text/plain")
	@ResponseBody
	public String updateRolet(String roleId,String roleName){
		if(roleId==""||roleId==null){
			return "3";
		}
		if(roleName.length()>50){
			return "0";
		}
		if(roleId!=""&&roleId!=null&&roleName!=""&&roleName!=null){
			roleService.updateRoleName(roleId, roleName);
			return "1";
		}else{
			return "2";
		}
		
	}
	//前往新增页面
	@RequestMapping("add")
	public String add(String roleName,Model model){
		model.addAttribute("roleName", roleName);
		return "role/addRole";
	}
	//新增角色
	@RequestMapping(value ="addRole",method=RequestMethod.GET,produces = "text/plain")
	@ResponseBody
	public String addRolet(String roleName){
		if(roleName==""||roleName==null){
			return "1";
		}
		if(roleName.length()>50){
			return "0";
		}
		Integer i=roleService.findExistRoleName(roleName);
		if(i!=0){
			return "2";
		}
		RoleEntity Role=new RoleEntity();
		Role.setRoleId(roleService.findMaxRoleId()+1);
		Role.setRoleName(roleName);
		roleService.insertRole(Role);
		return "3";
	}
	//下拉框显示角色名字
	@RequestMapping(value="/roleName",method={RequestMethod.GET})
	@ResponseBody
	public List<RoleEntity> roleName(){
		List<RoleEntity>list=new ArrayList<>();
		RoleEntity role=new RoleEntity();
		role.setRoleId(null);
		role.setRoleName("请选择");
		list.add(role);
		list.addAll(roleService.findAllRoleName());
		return list;
	}
	//查询该角色功能
	@RequestMapping("funButton")
	@ResponseBody
	public List<FunButtonEntity> funButton(String roleId){
		List<FunButtonEntity> list=roleService.findAllFunButton();
		if(roleId!=""&&roleId!=null){
			String[] arr=roleService.findUseFul(roleId);
			System.out.println(Arrays.toString(arr));
			for(int i=0;i<list.size();i++){
				for(int j=0;j<arr.length;j++){
					if(list.get(i).getButtonId().equals(arr[j])){
						list.get(i).setUseful(1);
					}
				}
				list.get(i).setMenuName(list.get(i).getMenuName1());
				if(list.get(i).getMenuName2()!=""&&list.get(i).getMenuName2()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName2());
				}
				if(list.get(i).getMenuName3()!=""&&list.get(i).getMenuName3()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName3());
				}
				if(list.get(i).getMenuName4()!=""&&list.get(i).getMenuName4()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName4());
				}
			}
		}else{
			for(int i=0;i<list.size();i++){
				list.get(i).setMenuName(list.get(i).getMenuName1());
				if(list.get(i).getMenuName2()!=""&&list.get(i).getMenuName2()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName2());
				}
				if(list.get(i).getMenuName3()!=""&&list.get(i).getMenuName3()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName3());
				}
				if(list.get(i).getMenuName4()!=""&&list.get(i).getMenuName4()!=null){	
					list.get(i).setMenuName(list.get(i).getMenuName()+"/"+list.get(i).getMenuName4());
				}
			}
		}
		
		return list;
	}
	//修改功能
	@RequestMapping(value ="updateFunButton",method=RequestMethod.GET,produces = "text/plain")
	@ResponseBody
	public String updateFunButton(String roleId,String buttonIds){
		if(roleId==null||roleId==""){
			return "0";
		}
		String[] strs=buttonIds.split(",");
		roleService.updateRoleFun(roleId);
		for(int i=0;i<strs.length;i++){
			FunRoleEntity FunRole=new FunRoleEntity();
			FunRole.setRoleId(roleId);
			FunRole.setFunId(strs[i]);
			roleService.insertRoleFun(FunRole);
		}
		return "1";
	}
}
